<template>
  <div class="fail">
    <div class="failWord">{{ $t('cpuNet.failTip') }}</div>
    <div class="flexb failBalance">
      <span>{{ $t('cpuNet.withdrawableAmount') }}</span>
      <span class="color-green">{{toFixed(fundBalance, 4)}} EOS</span>
    </div>
    <el-button type="primary" class="allBtn" v-loading="loading" @click="handleWithdrawAction" :disabled="disabled">
      {{ $t('cpuNet.totalExtraction') }}
    </el-button>
  </div>
</template>

<script>
import { toFixed } from '@/utils/validate';
import { EosModel } from '@/utils/eos';

export default {
  name: 'fail', // 续租失败弹窗
  data() {
    return {
      loading: false,
      fundBalance: 0,
      disabled: false
    }
  },
  components: {},
  props: {
    failVisible: {
      type: Boolean,
      default: false,
    },
    symbolScience: String,
  },
  watch: {
    failVisible: function change(val) {
      if (val) {
        this.handleGetRexBlance();
      }
    },
  },
  computed: {},
  created() {},
  mounted() {
    this.handleGetRexBlance();
  },
  beforeDestroy() {},
  methods: {
    handleWithdrawAction() {
      this.loading = true;
      const params = {
        amount: `${toFixed(this.fundBalance, 4)} ${this.symbolScience}`
      }
      EosModel.withdrawTransaction(params, (res) => {
        this.loading = false;
        if (res && res.broadcast) {
          this.handleGetRexBlance();
          this.$message.success(this.$t('newpos.successfulOperation'));
          this.$emit('listenFail', 'refresh');
          return;
        }
        EosModel.handleScatterErrorBack(res);
      })
    },
    handleGetRexBlance() {
      // 获取储备金余额
      EosModel.getRexFundBlance((row) => {
        if (!row.rows.length) {
          this.fundBalance = 0;
        } else {
          this.fundBalance = parseFloat(row.rows[0].balance, 0);
        }
        if (this.fundBalance === 0) {
          this.disabled = true;
        }
      })
    },
    // 数字截取 - 保留小数
    toFixed(num, p) {
      return toFixed(num, p);
    },
  },
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/variables.scss";
.color-green {
  color: $-btn-green;
}

.fail {
  padding: 0 20px;

  .failWord {
    margin-bottom: 30px;
  }

  .failBalance {
  }

  .allBtn {
    width: 100%;
    padding: 10px 0;
    color: #fff;
    margin-top: 30px;
  }

  /deep/ .el-dialog__header {
    padding-top: 30px;
  }
}
</style>

